﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using WebManager;
using Maticsoft.DBUtility;//请先添加引用

namespace BaoJian.WebManager.eshop
{
    public partial class ecolumnnew : System.Web.UI.Page
    {
        private string ColumnID = null;
        
    
        protected void Page_Load(object sender, EventArgs e)
        {
            ColumnID = HttpContext.Current.Request.QueryString["ColumnID"];
            if (!Page.IsPostBack)
            {
                //产品名称         
                LYQ.BLL.EProduct bll = new LYQ.BLL.EProduct();
                drpPName.DataSource = bll.GetList("");
                drpPName.DataBind();
                ListItem item = new ListItem("请选择", "-1");
                drpPName.Items.Insert(0, item);
            }
        }

        protected void drpPName_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitProduct();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
           string sql = "SELECT count(*) FROM  [EColumn] WHERE [PID]='{0}' AND [EColumnID]='{1}'";
           sql = string.Format(sql, ViewState["SelectPID"] as string, ColumnID);
           if (Convert.ToInt32(DbHelperSQL.GetSingle(sql)) != 0)
           {
               Msg.Text = "该栏目中已经存在该商品";
               return;
           }
            LYQ.BLL.EColumn bll = new LYQ.BLL.EColumn();
            LYQ.Model.EColumn model = new LYQ.Model.EColumn();

            model.SID = Tools.Common.GetMaxSIDAddedONE("EColumn");
            model.Description = txtDescription.Text;
            model.EPID = Guid.NewGuid().ToString();
            model.PID = ViewState["SelectPID"] as string;
            model.EColumnID = ColumnID;

            bll.Add(model);

            string url = URLUtility.UrlBase + "/WebManager/eshop/ecolumnlist.aspx?oid=" + ColumnID;
            Page.Response.Redirect(url);

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            string url = URLUtility.UrlBase + "/WebManager/eshop/ecolumnlist.aspx?oid=" + ColumnID;
            Page.Response.Redirect(url);

        }

        private void InitProduct()
        {

            LYQ.BLL.EProduct bll = new LYQ.BLL.EProduct();
            LYQ.Model.EProduct model = bll.GetModel(new Guid(drpPName.SelectedValue));


            ViewState["SelectPID"] = drpPName.SelectedValue;

            drpPName.DataSource = bll.GetList("").Tables[0].DefaultView;
            drpPName.DataBind();
            ListItem item = drpPName.Items.FindByValue(drpPName.SelectedValue);
            item.Selected = true;


            if (drpPName.SelectedValue != "-1")
            {
                Image1.ImageUrl = model.ImagePath;
            }
            else
            {
                Image1.ImageUrl = "";
            }

            txtPrice.Text = model.Price.ToString();
            txtMarketPrice.Text = model.MarketPrice.ToString();
            txtDiscount.Text = model.Discount.ToString();

            if (model.PCatalog1 != Guid.Empty)
            {
                item = PCatalog.Items.FindByValue(model.PCatalog1.ToString());
                item.Selected = true;
            }

            if (model.PCatalog2 != Guid.Empty)
            {
                item = PCatalog.Items.FindByValue(model.PCatalog2.ToString());
                item.Selected = true;
            }

            if (model.PCatalog3 != Guid.Empty)
            {
                item = PCatalog.Items.FindByValue(model.PCatalog3.ToString());
                item.Selected = true;
            }

            if (model.PCatalog4 != Guid.Empty)
            {
                item = PCatalog.Items.FindByValue(model.PCatalog4.ToString());
                item.Selected = true;
            }
            chkAvailable.Checked = model.Available;  


        }
    }
}
